﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace MenuLib
{
    [Serializable]
    public class MenuItem : ISerializable
    {
        public MenuItem(string name) :
            this(name, name)
        {
        }

        public MenuItem(string name, string text)
        {
            Name = name;
            Text = text;
            SelectionText = string.Empty;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public string SelectionText
        {
            get { return selectionText; }
            set { selectionText = value; }
        }

        /// <summary>
        ///     gets/sets the owner menu
        /// </summary>
        public virtual Menu OwnerMenu
        {
            get { return ownerMenu; }
            set { ownerMenu = value; }
        }

        public virtual void Display()
        {
            Console.WriteLine("{0} {1} - {2}", SelectionText, Name, Text);
        }

        public event EventHandler Select;

        protected internal virtual void OnSelect(EventArgs e)
        {
            if (null != Select)
            {
                Select(this, e);
            }
        }

        private string name;
        private string text;
        private string selectionText;
        private Menu ownerMenu;

        #region ISerializable Members

        const string nameValueName = "Name";
        const string textValueName = "Text";
        const string selectionTextValueName = "SelectionText";
        const string ownerMenuValueName = "OwnerMenu";

        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(nameValueName, Name);
            info.AddValue(textValueName, Text);
            info.AddValue(selectionTextValueName, SelectionText);
            info.AddValue(ownerMenuValueName, OwnerMenu);
        }

        protected MenuItem(SerializationInfo info, StreamingContext context)
        {
            Name = info.GetString(nameValueName);
            Text = info.GetString(textValueName);
            SelectionText = info.GetString(selectionTextValueName);
            ownerMenu = (Menu)info.GetValue(ownerMenuValueName, typeof(Menu));
        }

        #endregion
    }
}
